package com.macro.mall.portal.controller.ums;

import com.github.pagehelper.PageInfo;
import com.macro.mall.commission.domain.UserAccount;
import com.macro.mall.commission.entity.UserAccountEntity;
import com.macro.mall.commission.service.UserAccountService;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.util.BigDecimalUtils;
import com.macro.mall.model.UmsMember;
import com.macro.mall.portal.controller.BaseController;
import com.macro.mall.portal.dto.ums.MemberWithdrawReqDto;
import com.macro.mall.portal.dto.ums.PagedQueryMemberWithdrawLogReqDto;
import com.macro.mall.ums.domain.MemberWithdrawLog;
import com.macro.mall.ums.service.withdraw.MemberWithdrawLogService;
import com.macro.mall.ums.service.withdraw.bo.PagedQueryMemberWithdrawLogInput;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 会员提现记录控制器
 *
 * @author xuyanjun
 * @date 2019-12-28
 */
@Api(tags = "MemberWithdrawLogController", description = "会员提现记录")
@Slf4j(topic = "MemberWithdrawLogController")
@RestController
@RequestMapping("/memberWithdrawLog")
public class MemberWithdrawLogController extends BaseController {

    @Autowired
    private MemberWithdrawLogService memberWithdrawLogService;

    @Autowired
    private UserAccountService userAccountService;

    /**
     * 提现
     *
     * @param reqDto reqDto
     * @return 会员提现记录
     */
    @ApiOperation("提现")
    @PostMapping("withdraw")
    public CommonResult<UserAccount> withdraw(@RequestBody MemberWithdrawReqDto reqDto) {
        UmsMember currentUser = getCurrentUser();

        memberWithdrawLogService.withdraw(currentUser.getId(), BigDecimalUtils.fromYuanToFeng(reqDto.getAmount()), reqDto.getAlipayAccount(), reqDto.getAlipayRealname());

        UserAccountEntity userAccountEntity = userAccountService.getByUserId(currentUser.getId());

        return CommonResult.success(userAccountService.entityToDomain(userAccountEntity));
    }


    /**
     * 分页查询会员提现记录
     *
     * @return 分页后的会员提现记录
     */
    @ApiOperation("分页查询会员提现记录")
    @PostMapping("pagedQuery")
    public CommonPage<MemberWithdrawLog> pagedQuery(@RequestBody PagedQueryMemberWithdrawLogReqDto reqDto) {
        UmsMember currentUser = getCurrentUser();

        PagedQueryMemberWithdrawLogInput pagedQueryMemberWithdrawLogInput = new PagedQueryMemberWithdrawLogInput();
        pagedQueryMemberWithdrawLogInput
                .setMemberId(currentUser.getId())
                .setPageIndex(reqDto.getPageIndex())
                .setPageSize(reqDto.getPageSize());

        PageInfo<MemberWithdrawLog> pageInfo = memberWithdrawLogService.pagedQuery(pagedQueryMemberWithdrawLogInput);

        return CommonPage.restPage(pageInfo);
    }

}

